<?php
namespace application\datatypes;
use application\datatypes\DataType;

class LongText extends DataType {
	public static $name = 'Long Text';
	public static $fields = array(array('columnType'=>'TEXT'));
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && !$data[$field->cf_column]) {
			$results['errors'][] = $field->cf_display_name.' is required.';
		}
		elseif($value = $data[$field->cf_column]) {
			$results['db_fields'][$field->cf_column] = $value;	
		}
		
		return $results;
	}
	
	
	public static function formHeader($field) {
		if($field->cf_aux == 'wysiwyg') :
		Modulator()->includeScript(getCommonFile('third-party/tiny_mce/jquery.tinymce.js'));
		?>
		
		
		<script type="text/javascript">
			$(document).ready(function() {
				$('#<?=$field->cf_column?>').tinymce({
					// Location of TinyMCE script
					script_url : '<?=getCommonFile('third-party/tiny_mce/tiny_mce.js')?>',
					theme : "advanced",

					theme_advanced_buttons1 : "bold,italic,underline,|,formatselect,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,image,cleanup,code",
					theme_advanced_buttons2:'',
					theme_advanced_buttons3:'',
					theme_advanced_buttons4:'',
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
				});

			});
		</script>
		<?php
		endif;
			
		
	}
	
	public static function editForm($field) {
		?>
		<input type="hidden" name="field_id" value="<?=$field->getId()?>"/>
		<ul class="form-fields">
			<li>
				<label>Field Display Name</label>
				<input type="text" name="field_display_name" class="big wide" value="<?=$field->getDisplayName()?>"/>
			</li>
			<li>
				<label>Field Description</label>
				<textarea name="field_description" class="wide"><?=$field->getDescription()?></textarea>
			</li>
			<li>
				<label><input type="checkbox" name="field_required" <?=($field->getRequired()) ? 'checked="checked"' : ''?> value="1"/> Required</label>
				
			</li>
			<li>
				<label><input type="radio" name="field_aux" checked="checked" value="wysiwyg"/> WYSIWYG (What you see is what you get)</label>
				<label><input type="radio" <?php if($field->getAux() == 'none' || !$field->getAux()) echo 'checked="checked"';?> name="field_aux" value="none"/> None</label>
			</li>
			
		</ul>
		<?php
	}
		
	public static function display($formElements) {
		?>
		
		<fieldset class="single-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
			</section>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = $data[$field->cf_column];
		
		switch($field->cf_aux) {
			case 'html':
				$class = 'html-editor';
				break;
			case 'wysiwyg':
				$class = 'wysiwyg-editor';
				break;
			default:
				$class = 'long-text';
				break;
		}
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
				'element'=>
					sprintf('<textarea class="%1$s" name="%2$s" id="%2$s">%3$s</textarea>', 'editor', $field->cf_column, $value)
				
		));
		return $formElements;
	
	}
	
	public static function add() {
	
	}
	
	public static function save() {
		$editorType = $_REQUEST['editor_type'];
		$field = $_REQUEST['field'];
		DB()->query("UPDATE `cn_content_fields` SET `cf_aux`='$editorType' WHERE `cf_id`='$field'");
	}
	
	public static function delete() {
	
	}
		
	
}
?>